package org.eoti.rei.ndfcmd;

/**
 * The MIT License
 * <p/>
 * Copyright (c) 2009 Malachi de AElfweald (malachid@gmail.com)
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import org.eoti.io.xls.XLSColumn;
import org.eoti.io.xls.XLSReader;

import java.util.*;
import java.io.IOException;

public class DetailCmd
	extends NDFBrowserCommand
{
	public DetailCmd(){}

	public String[] commands()
	{
		return new String[]{"detail","info"};
	}

	public void process( List<String> commandLine)
			throws Exception
	{
		XLSReader reader = browser.getReader();
		if(reader == null)
		{
			format("No data loaded. Please try 'load filename'.\n");
			return;
		}

		if(commandLine.size() != 0)
		{
			detail(Integer.parseInt(commandLine.get(0)));
			return;
		}

		format("\n");
		for(int rowNum : reader.rows())
			format("%s. %s\n", rowNum, getAddress(rowNum));

		String input = readLine("Choose House Number> ");
		detail(Integer.parseInt(input));
	}

	public void detail(int houseNum)
			throws IOException
	{
		XLSReader reader = browser.getReader();
		format("\nHouse #%s Detail:\n", houseNum);
		for(XLSColumn column : reader.columns())
		{
			String s = column.getString(houseNum);
			if(s == null) continue;
			if(s.trim().length() == 0) continue;
			format("%-35s:\t%s\n", column, s);
		}

//			System.out.format("InterMap:\n  %s\n", getInterMap(houseNum));
	}

/*
	public String getInterMap(int rowNum)
	{
		// currently not working
		return String.format(
			"http://washims.co.washington.or.us/InterMap/InterDetails.cfm?StreetNum=%s&StreetName=%s&TLNO=%s&Account=%s&GoNav=2&CFID=295923&CFTOKEN=e9e3194bbd20cfcd-6BB1B35F-BCD6-9DF3-5CC9AEA307B8AE7D&jsessionid=4c30cf96128d1d14112d\n",
			readCell(rowNum, 0),
			readCell(rowNum, 1).replaceAll(" ", "%20"),
			readCell(rowNum, findColumn("Alt/Old APN")).toUpperCase().replaceAll("-", ""),
			readCell(rowNum, findColumn("APN"))
		);
	}
*/
	public void displayHelp()
	{
		format("detail: list available houses and prompt for house to display\n");
		format("detail houseNumber: display information for house\n");
		format("info: list available houses and prompt for house to display\n");
		format("info houseNumber: display information for house\n");
	}
}
